//
//  ZJMoveOptionsCell.swift
//  MoveOptions
//
//  Created by zhengju on 2017/8/1.
//  Copyright © 2017年 zhengju. All rights reserved.
//

import UIKit

class ZJMoveOptionsModel: NSObject {
    var title: String?
    var isLongPress: Bool?
    var isDisplayDelete:Bool?
    func initModel(title:String,isLong:Bool,disPlayDelete:Bool){
    
        self.title = title
        self.isLongPress = isLong
        self.isDisplayDelete = disPlayDelete
      
    }
}



class ZJMoveOptionsCell: UICollectionViewCell {
    
    var deleteAction:(()->())?
    var longAction:(()->())?
    var clickAction:(()->())?
    
    var _model:ZJMoveOptionsModel?
    
    var btn: UIButton!
    var deleteBtn: UIButton!

    var model:ZJMoveOptionsModel?{
        set{
        
            _model = newValue
            
            btn.setTitle(_model?.title, for: .normal)
           
            deleteBtn.isHidden = !(_model?.isDisplayDelete)!
            
        }
        get{

            return _model
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.backgroundColor =  UIColor.white
        
        btn = UIButton()
        btn.frame = self.bounds
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.addTarget(self, action: #selector(btnClick(button:)), for: .touchUpInside)
        self.contentView.addSubview(btn)
        
        let longPressGesture = UILongPressGestureRecognizer();
        longPressGesture.addTarget(self, action: #selector(longPressAction(longPress:)))
        longPressGesture.minimumPressDuration = 0.8; //定义按的时间
        btn?.addGestureRecognizer(longPressGesture)

        deleteBtn = UIButton()
        deleteBtn.frame = CGRect.init(x: self.bounds.size.width - 20, y: -15, width: 35, height: 35)
        deleteBtn.addTarget(self, action: #selector(deleteClick(button:)), for: .touchUpInside)
        deleteBtn.setImage(UIImage.init(named: "删除"), for: .normal)
        deleteBtn.isHidden = true
        self.contentView.addSubview(deleteBtn)
        
    }
    func longPressAction(longPress: UILongPressGestureRecognizer)  {
        
        if _model?.isLongPress == false{//如果是单击事件，长按手势不执行
        
            return
        }
        if longAction != nil {
            longAction!()
        }
    }
    func deleteClick(button:UIButton) {
        if deleteAction != nil {
            deleteAction!()
        }
    }
    func btnClick(button:UIButton) {
        
        if (_model?.isLongPress)! {//长按
            
            return
        }
        if clickAction != nil {
            clickAction!()
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
